package org.yutian.springboot.jdbc;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JDBCDemo {

    @Test
    public void jdbcConn() throws Exception {
        //下面方法有不同的异常，我直接抛出一个大的异常
        Connection con = null;
        Statement stat = null;
        try {
            //1、导入驱动jar包
            //2、注册驱动
            Class.forName("com.mysql.jdbc.Driver");

            //3、获取数据库的连接对象
            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/testyt?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "root", "123456");

            //5、获取执行sql语句的对象
            stat = con.createStatement();

            //4、定义sql语句
            String sql = "update Student set age = 99 where id = 1 ";

            //6、执行sql并接收返回结果
            int count = stat.executeUpdate(sql);

            //7、处理结果
            System.out.println(count);
        } finally {
            //8、释放资源
            if(null != stat){
                stat.close();
            }
            if(null != con){
                con.close();
            }
        }
    }
}